//
//  SelectDistanceView.m
//  OrderSeat
//
//  Created by 王威 on 13-11-27.
//  Copyright (c) 2013年 wangwei. All rights reserved.
//

#import "SelectDistanceView.h"
#import "DbManager.h"

@implementation SelectDistanceView

- (id)initWithFrame:(CGRect)frame
{
    if (SystemVersion < SystemVersion_7_0) frame.origin.y -= 64;
    self = [super initWithFrame:frame];
    if (self) {
        self.hidden = YES;
        
        _dataSource = [[SelectDistanceSource alloc] initWithView:self];
        
        _distanceTable = [[UITableView alloc] initWithFrame:frame];
        _distanceTable.top = 0;
        _distanceTable.dataSource = _dataSource;
        _distanceTable.delegate = _dataSource;
        _distanceTable.backgroundColor = [UIColor whiteColor];
        [self addSubview:_distanceTable];
        
        self.backgroundColor = [UIView colorWithHexString:@"#F7F7F7"];
        [self drawBorderWidth:1];
    }
    return self;
}

- (void)layoutSubviews
{
    [super layoutSubviews];
    _distanceTable.height = self.height;
}

- (void)resetAttributesWithTop:(CGFloat)top
{
    CLog(@"viewHeight:%g",self.viewHeight);
    self.height = self.hidden ? 0 : self.viewHeight;
    self.top = self.hidden ? (TOP_V7(top)-self.height) : TOP_V7(top);
}

- (void)showOrVisible
{
    [UIView animateWithDuration:0.4 animations:^(void) {
        self.hidden = !self.hidden;
        [self resetAttributesWithTop:46];
    }];
}

- (void)hidden
{
    [UIView animateWithDuration:0.4 animations:^(void) {
        self.hidden = YES;
        [self resetAttributesWithTop:46];
    }];
}

@end

//==============================================================================
#pragma ===================数据源类实现===========================================
//==============================================================================

@implementation SelectDistanceSource

- (id)initWithView:(id)view
{
    self = [super init];
    if(self) {
        self.view = view;
        [self initDistances];
    }
    
    return self;
}

- (void)initDistances
{
    _distanceList = [[DbManager sharedManager] fetchDistances];
    self.view.viewHeight = MIN(self.distanceList.count * 40, ContentAllHeight-46);
}

#pragma ===================加载tableview数据 Begin=================================

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 40;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return _distanceList.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *CellIdentifier = @"ShopInfoCell";
    
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    if (cell == nil) {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
        [cell setBackgroundColor:[UIColor clearColor]];
        cell.textLabel.textColor = [UIView colorWithHexString:@"#666666"];
        cell.textLabel.font  = [UIFont boldSystemFontOfSize:18];
    }
    
    NSDictionary *dic = [_distanceList objectAtIndex:indexPath.row];
    cell.textLabel.text = [dic objectForKey:@"display"];
    
    return cell;
}

#pragma mark - Table view delegate
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    [self.view.delegate selectDistanceFinished:[self.distanceList objectAtIndex:indexPath.row]];
    [self.view hidden];
}

#pragma ===================加载tableview数据 End================================================

@end
